package com.xxg.renrenorder.controller;

import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.domain.result.PageInfoResult;
import com.xxg.renrenorder.dao.bean.AfterSalesOrderInfo;
import com.xxg.renrenorder.domain.query.AfterSalesOrderListQuery;
import com.xxg.renrenorder.domain.query.OrderQuery;
import com.xxg.renrenorder.domain.query.VerifyOrderQuery;
import com.xxg.renrenorder.service.AfterSalesOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单售后管理接口
 *
 * @author szj
 * @date 2021/11/29
 */
@Api(tags = "订单售后管理")
@RestController
@RequestMapping("/afterOrder")
@AllArgsConstructor
public class AfterOrderController {
    private final AfterSalesOrderService afterSalesOrderService;

    @ApiOperation("订单申请售后")
    @RequestMapping(value = "applyAfterSalesOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "afterSalesOrderInfo", value = "售后信息", required = true, dataType = "AfterSalesOrderInfo")})
    public BaseResult<Void> applyAfterSalesOrder(@RequestBody @Validated AfterSalesOrderInfo afterSalesOrderInfo, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        afterSalesOrderInfo.setUserId(Integer.parseInt(userId));
        return afterSalesOrderService.applyAfterSalesOrder(afterSalesOrderInfo);
    }

    @ApiOperation("修改售后订单")
    @RequestMapping(value = "editAfterSalesOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "afterSalesOrderInfo", value = "售后信息", required = true, dataType = "AfterSalesOrderInfo")})
    public BaseResult<Void> editAfterSalesOrder(@RequestBody @Validated AfterSalesOrderInfo afterSalesOrderInfo, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return afterSalesOrderService.editAfterSalesOrder(afterSalesOrderInfo);
    }

    @ApiOperation("撤销售后申请")
    @RequestMapping(value = "cancelAfterSalesOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "售后订单ID", required = true, dataType = "com.dlrs.jz.ordermanager.domain.query.OrderQuery")})
    public BaseResult<Void> cancelAfterSalesOrder(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return afterSalesOrderService.cancelAfterSalesOrder(orderQuery.getId(), userId);
    }

    @ApiOperation("根据订单ID查看关联的所有售后订单详情")
    @RequestMapping(value = "getAfterOrderByOrderId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "1订单ID", required = true, dataType = "com.dlrs.jz.ordermanager.domain.query.OrderQuery")})
    public BaseResult<List<AfterSalesOrderInfo>> getAfterOrderByOrderId(@RequestBody @Validated OrderQuery orderQuery) {
        return afterSalesOrderService.getAfterOrderByOrderId(orderQuery.getId());
    }

    @ApiOperation("后台接口审核售后申请（1-等待退货,2-退款中,4-审核不通过,9-永久拒绝）")
    @RequestMapping(value = "verifyAfterSalesOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "verifyOrderQuery", value = "审核信息", required = true, dataType = "VerifyOrderQuery")})
    public BaseResult<Void> verifyAfterSalesOrder(@RequestBody @Validated VerifyOrderQuery verifyOrderQuery) {
        if (verifyOrderQuery.getLessQuantity() == null) {
            verifyOrderQuery.setLessQuantity(false);
        }
        return afterSalesOrderService.verifyAfterSalesOrder(verifyOrderQuery);
    }

    @ApiOperation("分页获取售后订单")
    @RequestMapping(value = "getAfterSalesOrders", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "afterSalesOrderListQuery", value = "分页条件", required = true, dataType = "AfterSalesOrderListQuery")})
    public BaseResult<List<AfterSalesOrderInfo>> getAfterSalesOrders(@RequestBody @Validated AfterSalesOrderListQuery afterSalesOrderListQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return afterSalesOrderService.getAfterSalesOrders(userId, afterSalesOrderListQuery.getPageNo(),
                afterSalesOrderListQuery.getPageSize());
    }

    @ApiOperation("查看售后订单详情")
    @RequestMapping(value = "getAfterSalesOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "2订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<AfterSalesOrderInfo> getAfterSalesOrderById(@RequestBody @Validated OrderQuery orderQuery) {
        return afterSalesOrderService.getAfterSalesOrderById(orderQuery.getId());
    }

    @ApiOperation("删除售后订单")
    @RequestMapping(value = "removeAfterSalesOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "4订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<Void> removeAfterSalesOrderById(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return afterSalesOrderService.removeAfterSalesOrderById(orderQuery.getId(), userId);
    }

    @ApiOperation("后台接口分页获取所有售后订单")
    @RequestMapping(value = "getAllAfterSalesOrdersByStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "afterSalesOrderListQuery", value = "分页条件", required = true, dataType = "AfterSalesOrderListQuery")})
    public BaseResult<PageInfoResult<AfterSalesOrderInfo>> getAllAfterSalesOrdersByStatus(@RequestBody @Validated AfterSalesOrderListQuery afterSalesOrderListQuery) {
        return afterSalesOrderService.getAllAfterSalesOrdersByStatus(afterSalesOrderListQuery);
    }

}
